package com.store.serviceimpl;

import com.store.mapper.AdminMapper;
import com.store.pojo.Admin;
import com.store.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ModelAndView loginValidate(Admin admin, ModelAndView modelAndView, HttpSession session) {
        String name = admin.getName();
        String password = admin.getPassword();
        if(StringUtils.isEmpty(name)){
            modelAndView.addObject("msg","用户名不能为空");
            modelAndView.setViewName("login");
            return modelAndView;
        }
        if(StringUtils.isEmpty(password)){
            modelAndView.addObject("msg","密码不能为空");
            modelAndView.setViewName("login");
            return modelAndView;
        }
        Admin admin1 = adminMapper.findUserByUsernameAndPassword(name,password);
        if(admin1==null){
            modelAndView.addObject("msg","无此管理员，请重新登录");
            modelAndView.setViewName("login");
            return modelAndView;
        }
        session.setAttribute("aid",admin1.getAid());
        modelAndView.addObject("msg","管理员："+name+"登陆成功");
        modelAndView.setViewName("adminpage/index");
        return modelAndView;
    }

}